package com.atguigu.controller;

import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {

    @DubboReference
    private UserInfoService userInfoService;

    /*完成退出的处理器*/
    @RequestMapping("/logout")
    @ResponseBody
    public Result logout(HttpServletRequest request){
        request.getSession().removeAttribute("USER");
        return Result.ok();
    }


    /*完成登录的处理器*/
    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestBody LoginVo loginVo,HttpServletRequest request){
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();
        if(StringUtils.isEmpty(phone)|| StringUtils.isEmpty(password)){
            return Result.build(null,ResultCodeEnum.PARAM_ERROR);
        }
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if(null == userInfo){
            return Result.build(null,ResultCodeEnum.ACCOUNT_ERROR);
        }
        if(!MD5.encrypt(password).equals(userInfo.getPassword())){
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }
        if(userInfo.getStatus() ==0){
            return Result.build(null,ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        // 登录成功,将用户userInfo对象存入session
        request.getSession().setAttribute("USER",userInfo);
        // 响应 电话号和昵称
        Map<String,Object> map =new HashMap<>();
        map.put("phone",userInfo.getPhone());
        map.put("nickName",userInfo.getNickName());

        return Result.ok(map);

    }


    /*完成注册功能的处理器*/
    @RequestMapping("/register")
    @ResponseBody
    public Result register(@RequestBody RegisterVo registerVo,HttpServletRequest request){
        // 获取注册信息,校验是否为空
        String nickName = registerVo.getNickName();
        String code = registerVo.getCode();
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        if(StringUtils.isEmpty(nickName)|| StringUtils.isEmpty(code) || StringUtils.isEmpty(phone)|| StringUtils.isEmpty(password)){
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        // 校验验证码是否正确
        String currentCode =(String)request.getSession().getAttribute("CODE");
        if(! code.equals(currentCode)){
            return Result.build(null,ResultCodeEnum.CODE_ERROR);
        }
        // 手机号码是否被占用
        UserInfo userInfo =userInfoService.getByPhone(phone);
        if(null != userInfo){
            return Result.build(null,ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        // 保存注册信息进入数据库
        userInfo=new UserInfo();
        userInfo.setNickName(nickName);
        userInfo.setPhone(phone);
        userInfo.setStatus(1);
        userInfo.setPassword(MD5.encrypt(password));
        userInfoService.insert(userInfo);
        // 响应成功
        return Result.ok();
    }


    /*获取验证码的处理器*/

    @RequestMapping("/sendCode/{phone}")
    @ResponseBody
    public Result sendCode(@PathVariable("phone") String phone, HttpServletRequest request){
        String code ="1111";
        // 调用第三方服务,向手机发送验证码
        // 将验证码存入session
        HttpSession session = request.getSession();
        session.setAttribute("CODE",code);
        // 响应发送成功
        return Result.ok(code);

    }
}
